//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CommodityIndex.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Experimental/Commodities/CommodityType.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Experimental/Commodities/UnitOfMeasure.h>
#include <gen/QL/Experimental/Commodities/CommodityCurve.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityIndex::CCommodityIndex (boost::shared_ptr<QuantLib::CommodityIndex>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityIndex = NULL;
#endif
	_ppCommodityIndex = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CCommodityIndex::CCommodityIndex (QuantLib::CommodityIndex& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCommodityIndex = NULL;
#endif
	_ppCommodityIndex = new boost::shared_ptr<QuantLib::CommodityIndex> (&childNative);
    
    _CommodityIndexOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CCommodityIndex::CCommodityIndex (CCommodityIndex^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityIndex = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCommodityIndex = new boost::shared_ptr<QuantLib::CommodityIndex> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CCommodityIndex::CCommodityIndex (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityIndex = NULL;
#endif
	if (!t->IsSubclassOf(CCommodityIndex::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CCommodityIndex::CCommodityIndex (QuantLib::Handle<QuantLib::CommodityIndex>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommodityIndex = &childNative;
	_ppCommodityIndex = &static_cast<boost::shared_ptr<QuantLib::CommodityIndex>>(childNative.currentLink());
    
    _CommodityIndexOwner = owner;
}
Cephei::QL::Experimental::Commodities::CCommodityIndex::CCommodityIndex (QuantLib::Handle<QuantLib::CommodityIndex> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommodityIndex = &childNative;
	_ppCommodityIndex = &static_cast<boost::shared_ptr<QuantLib::CommodityIndex>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityIndex::CCommodityIndex (QuantLib::CommodityIndex childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityIndex = NULL;
#endif
	_ppCommodityIndex = new boost::shared_ptr<QuantLib::CommodityIndex> (new QuantLib::CommodityIndex (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CCommodityIndex::~CCommodityIndex ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCommodityIndex != NULL)
    {
	    delete _ppCommodityIndex;
        _ppCommodityIndex = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CCommodityIndex::!CCommodityIndex ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCommodityIndex != NULL)
    {
	    delete _ppCommodityIndex;
    }
}
QuantLib::CommodityIndex& Cephei::QL::Experimental::Commodities::CCommodityIndex::GetReference ()
{
    if (_ppCommodityIndex == NULL) throw gcnew NativeNullException ();
	return **_ppCommodityIndex;
}
boost::shared_ptr<QuantLib::CommodityIndex>& Cephei::QL::Experimental::Commodities::CCommodityIndex::GetShared ()
{
    if (_ppCommodityIndex == NULL) throw gcnew NativeNullException ();
	return *_ppCommodityIndex;
}
QuantLib::CommodityIndex* Cephei::QL::Experimental::Commodities::CCommodityIndex::GetPointer ()
{
    if (_ppCommodityIndex == NULL) throw gcnew NativeNullException ();
	return &**_ppCommodityIndex;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CommodityIndex>& Cephei::QL::Experimental::Commodities::CCommodityIndex::GetHandle ()
{
	if (_phCommodityIndex == NULL)
	{
		_phCommodityIndex = new Handle<QuantLib::CommodityIndex> (*_ppCommodityIndex);
	}
	return *_phCommodityIndex;
}
#endif
bool Cephei::QL::Experimental::Commodities::CCommodityIndex::HasNative () 
{
	return (_ppCommodityIndex != NULL);
}

Cephei::QL::Experimental::Commodities::ICommodityIndex^ Cephei::QL::Experimental::Commodities::CCommodityIndex::AddQuote (DateTime quoteDate, Double quote)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _quoteDate = (QuantLib::Date)ValueHelper::Convert (quoteDate);
        QuantLib::Real _quote = (QuantLib::Real)ValueHelper::Convert (quote);
    	(*_ppCommodityIndex)->addQuote ( _quoteDate,  _quote );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::Experimental::Commodities::CCommodityIndex::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar& _rv = (QuantLib::Calendar&)(*_ppCommodityIndex)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityIndex^ Cephei::QL::Experimental::Commodities::CCommodityIndex::ClearQuotes::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCommodityIndex)->clearQuotes ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityType^ Cephei::QL::Experimental::Commodities::CCommodityIndex::CommodityType::get ()
{
    try
    {
    	QuantLib::CommodityType& _rv = (QuantLib::CommodityType&)(*_ppCommodityIndex)->commodityType ( );   
        Cephei::QL::Experimental::Commodities::CCommodityType^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CCommodityType (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ICurrency^ Cephei::QL::Experimental::Commodities::CCommodityIndex::Currency::get ()
{
    try
    {
    	QuantLib::Currency& _rv = (QuantLib::Currency&)(*_ppCommodityIndex)->currency ( );   
        Cephei::QL::CCurrency^ _nrv = gcnew Cephei::QL::CCurrency (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Commodities::CCommodityIndex::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCommodityIndex)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityCurve^ Cephei::QL::Experimental::Commodities::CCommodityIndex::ForwardCurve::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::CommodityCurve>& _rv = (boost::shared_ptr<QuantLib::CommodityCurve>&)(*_ppCommodityIndex)->forwardCurve ( );   
        Cephei::QL::Experimental::Commodities::CCommodityCurve^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CCommodityCurve (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Commodities::CCommodityIndex::ForwardCurveEmpty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCommodityIndex)->forwardCurveEmpty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Commodities::CCommodityIndex::ForwardPrice (DateTime date)
{
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCommodityIndex)->forwardPrice ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Commodities::CCommodityIndex::IsValidQuoteDate (DateTime quoteDate)
{
    try
    {
        QuantLib::Date _quoteDate = (QuantLib::Date)ValueHelper::Convert (quoteDate);
    	bool _rv = (bool)(*_ppCommodityIndex)->isValidQuoteDate ( _quoteDate );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Commodities::CCommodityIndex::LastQuoteDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCommodityIndex)->lastQuoteDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Commodities::CCommodityIndex::LotQuantity::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCommodityIndex)->lotQuantity ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Experimental::Commodities::CCommodityIndex::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppCommodityIndex)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Commodities::CCommodityIndex::Price (DateTime date)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCommodityIndex)->price ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::IUnitOfMeasure^ Cephei::QL::Experimental::Commodities::CCommodityIndex::UnitOfMeasure::get ()
{
    try
    {
    	QuantLib::UnitOfMeasure& _rv = (QuantLib::UnitOfMeasure&)(*_ppCommodityIndex)->unitOfMeasure ( );   
        Cephei::QL::Experimental::Commodities::CUnitOfMeasure^ _nrv = gcnew Cephei::QL::Experimental::Commodities::CUnitOfMeasure (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::ICommodityIndex^ Cephei::QL::Experimental::Commodities::CCommodityIndex::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppCommodityIndex)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

